# encoding: utf-8
class TasksController < ApplicationController
  before_filter :is_member_of_project?
  before_filter :authenticate_user!
  before_filter :get_project
  before_filter :get_workpackage
  
  def index
    @tasks = Task.find_all_by_workpackage_id(params[:workpackage_id])
  
    respond_to do |format|
      format.html
      format.xml { render :xml => @task }
    end
  end

  def show
    @task = Task.find(params[:id])
    @processors = @task.processors
    
    respond_to do |format|
      format.html
      format.xml { render :xml => @task }
    end
  end

  def new
    if is_project_manager?
      @task = Task.new

      respond_to do |format|
        format.html
        format.xml { render :xml => @task }
      end
    else
      flash[:error] = t :task_no_right_new, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to :action => "index" }
        format.xml { render :xml => @task }
      end
    end
  end

  def create
    @task = Task.new(params[:task])
    @task.workpackage_id = params[:workpackage_id]

    ActiveRecord::Base.transaction do
      respond_to do |format|
        if @task.save
          set_progress
          flash[:notice] = t :task_create_success, :scope => [:flash, :notice]
          format.html { redirect_to(project_workpackage_task_path(params[:project_id], params[:workpackage_id], @task)) }
          format.xml { render :xml => @task, :status => :created, :location => @task }
        else
          format.html { render :action => "new" }
          format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
        end
      end
    end
  end

  def update
    @task = Task.find(params[:id])

    ActiveRecord::Base.transaction do
      respond_to do |format|
        if @task.update_attributes(params[:task])
          set_progress
          flash[:notice] = t :task_edit_success, :scope => [:flash, :notice]
          format.html { redirect_to(project_workpackage_task_path(params[:project_id], params[:workpackage_id], @task)) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
        end
      end
    end
  end

  def edit
    if is_project_manager?
      @task = Task.find(params[:id])
    else
      flash[:error] = t :task_no_rigth_edit, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to( project_workpackage_task_path(params[:project_id], params[:workpackage_id], params[:id]))}
        format.xml { render :xml => @task }
      end
    end
  end

  def destroy
    if is_project_manager?
      @task = Task.find(params[:id])
      ActiveRecord::Base.transaction do
        if @task.destroy
          set_progress
          flash[:notice] = t :task_delete_success, :scope => [:flash, :notice]
          respond_to do |format|
            format.html { redirect_to ( project_workpackage_path(params[:project_id], params[:workpackage_id]) ) }
            format.xml  { head :ok }
          end
        else
          flash[:error] = t :task_delete_error, :scope => [:flash, :error]
          redirect_to :action => "index"
        end
      end
    else
      flash[:error] = t :task_no_right_delete, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to :action => "index" }
        format.xml { render :xml => @task }
      end
    end
  end

end
